﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
using Ext.Net;

namespace Web.View.publishNews
{
    public partial class Editor : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                InfoBind();
        }
        private void InfoBind()
        {
            if(Request["id"]==null)return;
            newsinfoMgr mgr = new newsinfoMgr();
            newsInfo _newsinfo = mgr.FindById_newsinfo(int.Parse(Request["id"].ToString()));
            if (_newsinfo.newstitle != null)
            {
                this.TxtTitle.Text = _newsinfo.newstitle;
                this.TxtDesc.Text = _newsinfo.newsdesc;
                cboClassify.Text = _newsinfo.newsclassify;
                EditorContent.Text = _newsinfo.newscontent;
                TxtDesc.Text = _newsinfo.newsdesc;
            }
        }
        protected void BtnSave_DirectClick(object sender, Ext.Net.DirectEventArgs e)
        {
            if (TxtTitle.Text.Trim().Length <= 0 || EditorContent.Text.Trim().Length <= 0)
            {
                X.Msg.Notify("提示", "<font color='green'>标题和内容不能为空</font>").Show(); return;
            }
            newsinfoMgr mgr = new newsinfoMgr();
            newsInfo _newsinfo = new newsInfo();
            if (Request["id"]!=null)
            {
                _newsinfo = mgr.FindById_newsinfo(int.Parse(Request["id"].ToString()));
            }
            _newsinfo.newsclassify = cboClassify.SelectedItem.Text;
            _newsinfo.newstitle = TxtTitle.Text;
            _newsinfo.newscontent = EditorContent.Text;
            _newsinfo.newsdesc = TxtDesc.Text;
            _newsinfo.newsowner = Session["username"].ToString();
            _newsinfo.newstime = DateTime.Now.ToString();
            if (Request["id"] != null)
            {
                if (mgr.Update_newsinfo(_newsinfo))
                    X.Msg.Notify("成功", "<font color='green'>记录修改成功</font>").Show();
                else
                    X.Msg.Notify("失败", "<font color='green'>记录修改失败，请您重试！</font>").Show();
            }
            else
            {
                if (mgr.Add_newsinfo(_newsinfo))
                    X.Msg.Notify("成功", "<font color='green'>记录添加成功</font>").Show();
                else
                    X.Msg.Notify("失败", "<font color='green'>记录添加失败，请您重试！</font>").Show();
            }
        }
    }
}